package com.reiuy.controller;


import com.reiuy.entity.Cart;
import com.reiuy.entity.Good;
import com.reiuy.entity.Transaction;
import com.reiuy.entity.User;
import com.reiuy.exception.TransactionException;
import com.reiuy.service.CartService;
import com.reiuy.service.TransactionService;
import com.reiuy.service.UserService;
import com.reiuy.vo.Transactioninfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/transaction")
public class TranscationController {

    @Resource
    private TransactionService transactionService;

    @Resource
    private CartService cartService;
    @Resource
    private UserService userService;

    @RequestMapping(value = "/creattransactionfromshop.do",method = RequestMethod.GET)
    public ModelAndView creatTransactionfromshop(Good good, User user,Integer quantity){
        ModelAndView mv = new ModelAndView();
        String tips = "";
        Map<String, Integer> map = new HashMap<>();
        Transactioninfo transactioninfo = new Transactioninfo();

        map = transactionService.creattransactionbyshop(user,good,quantity);
        int nums = map.get("nums");
        Integer transactionid = map.get("transactionid");
        if (nums > 0){
            tips = "交易创建成功,请尽快进行支付";
            transactioninfo = transactionService.selecttransactioninfoById(transactionid);
            //作为页面处理,这里还需要传递一个值,transactioninfo用来在下一个页面里给用户回显当前交易的信息
            //这个逻辑分叉所产生的两个页面应该是不同的

            Float balance = userService.getuserbalance(user);
            //获取用户的余额

            mv.addObject("balance",balance);
            mv.addObject("transactioninfo",transactioninfo);
            mv.addObject("tips",tips);
            mv.setViewName("transaction/transactioncreatesuccess");

        }else if(nums == -1){
            tips = "用户不允许购买自己的商品";
            mv.addObject("tips",tips);
            mv.setViewName("transaction/transactioncreatefailed");
        }
        else{
            tips = "交易创建失败";
            mv.addObject("tips",tips);
            mv.setViewName("transaction/transactioncreatefailed");
        }
        return mv;
    }


    @RequestMapping(value = "/creattransactionfromcart.do",method = RequestMethod.GET)
    public ModelAndView creatTransactionfromcart(Cart cart, HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        Transactioninfo transactioninfo = new Transactioninfo();
        String tips = "";

        Map<String,Integer> map = new HashMap<>();
        map = transactionService.creattransactionbycart(cart);
        //此处传入的参数是一个携带着cartid的cart对象
        //在service对象中已经定义好了使用cartid寻找cart的dao方法
        //所以直接传入即可
        Integer nums = map.get("nums");
        Integer transactionid = map.get("transactionid");
        //这里从返回的map中拿到了代表该条交易的唯一标识transactionid

        if(nums > 0 ){
            tips = "交易创建成功,请尽快进行支付";
            cartService.deletecart(cart);
            //交易创建后,删除购物车中该条记录
            transactioninfo = transactionService.selecttransactioninfoById(transactionid);
            //获得交易的细节
            String username = (String) request.getSession().getAttribute("username");
            User user = new User();
            user.setUsername(username);
            Float balance = userService.getuserbalance(user);
            //获取用户的余额

            mv.addObject("transactioninfo",transactioninfo);
            mv.addObject("tips",tips);
            mv.addObject("balance",balance);
            mv.setViewName("transaction/transactioncreatesuccess");
        }else {
            tips = "交易创建失败";
            mv.addObject("tips",tips);
            mv.setViewName("transaction/transactioncreatefailed");
        }
        return mv;
    }

    @RequestMapping("/pay.do")
    public ModelAndView dopayment(Transaction transaction) throws TransactionException {
        ModelAndView mv = new ModelAndView();
        transactionService.dopayment(transaction.getTransactionid());
        String msg = "支付成功";
        mv.addObject("msg",msg);
        mv.setViewName("transaction/paysuccess");
        return mv;
    }

    @RequestMapping("/foundtransac.do")
    private ModelAndView foundtransac(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        String username = (String) request.getSession().getAttribute("username");
        List<Transactioninfo> paidtransactioninfos= transactionService.getUserpaidtransac(username);
        List<Transactioninfo> unpaidtransactioninfos= transactionService.getUserunpaidtransac(username);
        //分别获得已支付的订单和未支付的订单list集合

        mv.addObject("paidtransactioninfos",paidtransactioninfos);
        mv.addObject("unpaidtransactioninfos",unpaidtransactioninfos);
        mv.setViewName("transaction/showtransaction");

        return mv;
    }
}
